package org.Presentacion;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.Toolkit;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import java.awt.Color;

import javax.swing.JList;
import javax.swing.JTextField;

import java.awt.Font;
import java.util.*;

import javax.swing.JButton;

import org.Dominio.*;

import com.toedter.calendar.JCalendar;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class principal {

	private JFrame frmMoreCalendar;
	private final JLabel label = new JLabel("");
	private final JPanel panel = new JPanel();
	private final JLabel lblNombreEvento = new JLabel("Nombre evento:");
	private final JTextField textField = new JTextField();
	private final JLabel label_1 = new JLabel("More Calendar");
	private final JButton btnModificar = new JButton("Modificar");
	private final JButton btnAadir = new JButton("Anadir");
	private final JButton btnEliminar = new JButton("Eliminar");
	private final JPanel panel_1 = new JPanel();
	private final JLabel lblHora = new JLabel("Hora:");
	private final JScrollPane scrollPane = new JScrollPane();
	private final JTextArea textArea = new JTextArea();
	private final JSpinner spinner = new JSpinner();
	private final JSpinner spinner_1 = new JSpinner();
	private final JLabel label_2 = new JLabel(":");
	static String id;
	JCalendar cal;
	LinkedList<LinkedList<Object>> eventos;
	String fechaActual;
	DefaultListModel eventosList = new DefaultListModel();
	private EventoD event;
	private final JScrollPane scrollPane_1 = new JScrollPane();
	private final JList list = new JList();
	private final JButton btnGuardarEvento = new JButton("Guardar Evento");
	private final JButton btnCancelar = new JButton("Cancelar");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		id=args[0];
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					principal window = new principal();
					window.frmMoreCalendar.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public principal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		textField.setBounds(109, 21, 131, 20);
		textField.setColumns(10);
		frmMoreCalendar = new JFrame();
		frmMoreCalendar.setResizable(false);
		frmMoreCalendar.getContentPane().setBackground(new Color(175, 238, 238));
		frmMoreCalendar.setTitle("More Calendar");
		frmMoreCalendar.setIconImage(Toolkit.getDefaultToolkit().getImage(principal.class.getResource("/images/iconitos_004.jpg")));
		frmMoreCalendar.setBounds(100, 100, 621, 447);
		frmMoreCalendar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmMoreCalendar.getContentPane().setLayout(null);
		{
			label.setBounds(0, 0, 0, 0);
			frmMoreCalendar.getContentPane().add(label);
		}
		{
			panel.setBounds(302, 81, 270, 311);
			panel.setBackground(new Color(175, 238, 238));
			panel.setBorder(new TitledBorder(null, "Datos evento", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel.setLayout(null);
			frmMoreCalendar.getContentPane().add(panel);
		}
		{
			lblNombreEvento.setBounds(20, 24, 131, 14);
			panel.add(lblNombreEvento);
		}
		{
			panel.add(textField);
		}
		{
			btnModificar.setBounds(20, 243, 220, 23);
			panel.add(btnModificar);
		}
		{
			btnAadir.addActionListener(new BtnAadirActionListener());
			btnAadir.setBounds(20, 207, 220, 23);
			panel.add(btnAadir);
		}
		{
			btnEliminar.addActionListener(new BtnEliminarActionListener());
			btnEliminar.setBounds(20, 277, 220, 23);
			panel.add(btnEliminar);
		}
		{
			lblHora.setBounds(79, 64, 46, 14);
			panel.add(lblHora);
		}
		scrollPane.setBackground(new Color(175, 238, 238));
		scrollPane.setBorder(new TitledBorder(null, "Descripcion del Evento", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		scrollPane.setBounds(20, 100, 220, 84);
		
		panel.add(scrollPane);
		
		scrollPane.setViewportView(textArea);
		spinner.setModel(new SpinnerNumberModel(0, 0, 23, 1));
		spinner.setBounds(119, 58, 40, 20);
		
		panel.add(spinner);
		spinner_1.setModel(new SpinnerNumberModel(0, 0, 59, 1));
		spinner_1.setBounds(180, 58, 40, 20);
		
		panel.add(spinner_1);
		label_2.setBounds(169, 61, 46, 14);
		
		panel.add(label_2);
		{
			label_1.setForeground(new Color(255, 140, 0));
			label_1.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 34));
			label_1.setBounds(156, 11, 264, 47);
			frmMoreCalendar.getContentPane().add(label_1);
		}
		{	
			cal=new JCalendar();
			cal.addPropertyChangeListener(new CalPropertyChangeListener());
			panel_1.setBorder(new TitledBorder(null, "Calendario", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel_1.setBackground(new Color(175, 238, 238));
			
			panel_1.add(cal);
			panel_1.setBounds(29, 69, 242, 196);
			frmMoreCalendar.getContentPane().add(panel_1);
			
			textField.setEditable(false);
			textArea.setEditable(false);
			spinner.setEnabled(false);
			spinner_1.setEnabled(false);
			textField.setText(id);
		}
		btnGuardarEvento.addActionListener(new BtnGuardarEventoActionListener());
		btnGuardarEvento.setVisible(false);
		btnGuardarEvento.setBounds(20, 225, 220, 23);
		
		panel.add(btnGuardarEvento);
		btnCancelar.addActionListener(new BtnCancelarActionListener());
		btnCancelar.setVisible(false);
		btnCancelar.setBounds(20, 259, 220, 23);
		
		panel.add(btnCancelar);
		scrollPane_1.setBackground(new Color(175, 238, 238));
		scrollPane_1.setBorder(new TitledBorder(null, "Eventos", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		scrollPane_1.setBounds(29, 276, 242, 132);
		
		frmMoreCalendar.getContentPane().add(scrollPane_1);
		/*list.setModel(new AbstractListModel() {
			String[] values = new String[] {"mierda"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});*/
		list.addListSelectionListener(new ListListSelectionListener());
		
		list.setModel(eventosList);
		event=new EventoD(id);
		eventos=event.leerTodos();
		for(int i=0;i<eventos.size();i++){
			LinkedList<Object> p=eventos.get(i);
			if(p.get(0).toString().equals(fechaActual))
				eventosList.addElement(p.get(1).toString()+" "+p.get(2).toString());
		}
		scrollPane_1.setViewportView(list);
	}

	private class CalPropertyChangeListener implements PropertyChangeListener {
		public void propertyChange(PropertyChangeEvent arg0) {
			fechaActual=cal.getDate().getDate()+"/"+(cal.getDate().getMonth()+1)+"/"+(cal.getDate().getYear()+1900);
			eventosList.clear();
			textField.setText("");
			textArea.setText("");
			spinner.setValue(0);
			spinner_1.setValue(0);
			for(int i=0;i<eventos.size();i++){
				LinkedList<Object> p=eventos.get(i);
				if(p.get(0).toString().equals(fechaActual))
					eventosList.addElement(p.get(1).toString()+" "+p.get(2).toString());
			}
		}
	}
	private class ListListSelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent arg0) {
			int indice=list.getSelectedIndex(), j=-1;
			for(int i=0;i<eventos.size();i++){
				LinkedList<Object> p=eventos.get(i);
				if(p.get(0).toString().equals(fechaActual)){
					j++;
					if(j==indice){
						textField.setText(p.get(2).toString());
						textArea.setText(p.get(3).toString());
						String[] horaCompleta=p.get(4).toString().split(":");
						
						spinner.setValue(Integer.parseInt(horaCompleta[0]));
						spinner_1.setValue(Integer.parseInt(horaCompleta[1]));
					}
						
				}			
				
			}
		}
	}
	private class BtnAadirActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			btnAadir.setVisible(false);
			btnModificar.setVisible(false);
			btnEliminar.setVisible(false);
			btnGuardarEvento.setVisible(true);
			btnCancelar.setVisible(true);
			textField.setEditable(true);
			textArea.setEditable(true);
			spinner.setEnabled(true);
			spinner_1.setEnabled(true);
			list.clearSelection();
			list.setEnabled(false);
			textField.setText("");
			textArea.setText("");
			spinner.setValue(0);
			spinner_1.setValue(0);
			cal.setEnabled(false);
		}
	}
	private class BtnCancelarActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			btnAadir.setVisible(true);
			btnModificar.setVisible(true);
			btnEliminar.setVisible(true);
			btnGuardarEvento.setVisible(false);
			btnCancelar.setVisible(false);
			textField.setEditable(false);
			textArea.setEditable(false);
			spinner.setEnabled(false);
			spinner_1.setEnabled(false);
			list.setEnabled(true);
			textField.setText("");
			textArea.setText("");
			spinner.setValue(0);
			spinner_1.setValue(0);
		}
	}
	private class BtnGuardarEventoActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String ide=(Integer.parseInt(eventos.get(eventos.size()-1).get(1).toString())+1)+"";
			EventoD eventico=new EventoD(ide,fechaActual,textField.getText(),textArea.getText(),spinner.getValue()+":"+spinner_1.getValue(),id);
			eventico.insertarEvento();
			btnAadir.setVisible(true);
			btnModificar.setVisible(true);
			btnEliminar.setVisible(true);
			btnGuardarEvento.setVisible(false);
			btnCancelar.setVisible(false);
			textField.setEditable(false);
			textArea.setEditable(false);
			spinner.setEnabled(false);
			spinner_1.setEnabled(false);
			list.setEnabled(true);
			textField.setText("");
			textArea.setText("");
			spinner.setValue(0);
			spinner_1.setValue(0);
			eventos=event.leerTodos();
			for(int i=0;i<eventos.size();i++){
				LinkedList<Object> p=eventos.get(i);
				if(p.get(0).toString().equals(fechaActual))
					eventosList.addElement(p.get(1).toString()+" "+p.get(2).toString());
			}
			cal.setEnabled(true);
		}
	}
	private class BtnEliminarActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(!list.isSelectionEmpty()){
				int opcion=JOptionPane.showOptionDialog(frmMoreCalendar, "Seguro que quieres eliminar este evento?", "Eliminar Evento", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
				if(opcion==0){		
				int indice=list.getSelectedIndex(), j=-1;
				for(int i=0;i<eventos.size();i++){
					LinkedList<Object> p=eventos.get(i);
					if(p.get(0).toString().equals(fechaActual)){
						j++;
						if(j==indice){
							EventoD eventillo=new EventoD();
							eventillo.set_id(p.get(1).toString());
							eventillo.eliminarEvento();
						}
							
					}			
					
				}
				eventosList.clear();
				eventos=event.leerTodos();
				for(int i=0;i<eventos.size();i++){
					LinkedList<Object> p=eventos.get(i);
					if(p.get(0).toString().equals(fechaActual))
						eventosList.addElement(p.get(1).toString()+" "+p.get(2).toString());
				}
				textField.setText("");
				textArea.setText("");
				spinner.setValue(0);
				spinner_1.setValue(0);
				}
					
			}
		}
		
	}
}
